package com.xiaotao.controller;

import com.xiaotao.dto.GroupBuyDto;
import com.xiaotao.dto.OrderRequest;
import com.xiaotao.entity.Orders;
import com.xiaotao.service.OrderService;
import com.xiaotao.util.ResponseEnum;
import com.xiaotao.util.ResponseUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/orders")
@RequiredArgsConstructor
public class OrderController {
    @Resource
    private OrderService orderService;


    /** 创建普通订单（支持优惠券） */
    @PostMapping("/create/coupon")
    public ResponseUtil createOrder(@RequestBody OrderRequest request) throws Exception {
        return orderService.createOrderWithCoupon(request);
    }

    /** 取消订单 */
    @PostMapping("/{orderId}/cancel")
    public ResponseUtil cancelOrder(@PathVariable String orderId) {
        orderService.cancelOrder(orderId);
        return ResponseUtil.get(ResponseEnum.OK);
    }

    /** 查询订单状态 */
    @GetMapping("/{orderId}")
    public ResponseUtil getOrder(@PathVariable String orderId) {
        // 你自己写 getById
        ResponseUtil responseUtil = orderService.verifyOrder(orderId);

        return ResponseUtil.get(ResponseEnum.OK,responseUtil);
    }
    /** 创建团购订单 */
    @PostMapping("/create/group")
    public ResponseUtil createGroupOrder(@RequestBody GroupBuyDto req) throws Exception {
        return orderService.createGroupOrder(req);
    }
    @GetMapping
    public ResponseUtil queryAll() {
        ResponseUtil responseUtil = orderService.queryAll();
        return ResponseUtil.get(ResponseEnum.OK,responseUtil);
    }
    @DeleteMapping("/{orderId}")
    public ResponseUtil delete(@PathVariable String orderId) {
        ResponseUtil responseUtil = orderService.delete(orderId);
        return ResponseUtil.get(ResponseEnum.OK,responseUtil);
    }
    @PutMapping()
    public ResponseUtil update(@RequestBody Orders orders) {
        ResponseUtil responseUtil = orderService.update(orders);
        return ResponseUtil.get(ResponseEnum.OK,responseUtil);
    }




}
